<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" placeholder="绑定的手机号" v-model="form.mobile" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="短信验证码" v-model="form.code" />
        <span class="code" @click="send">{{countTime === 0 ? '发送验证码' : countTime + '秒后重试'}}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { reactive, ref } from 'vue'
import { message } from '@/components/Message'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { useCountDown } from '@/compositions'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  props: {
    openId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 已注册未绑定   是来绑定手机号的
    // qq名称
    const nickname = ref('')
    const store = useStore()
    const router = useRouter()
    // qq头像
    const avatar = ref('')
    console.log('QC', QC)
    console.log('判断登录成功了么', QC.Login.check())
    // 如果登录成功需要查一下用户qq信息
    if (QC.Login.check()) {
      // 获取qq头像和名称用于页面渲染
      QC.api('get_user_info').success((res) => {
        console.log('用户基本信息', res)
        nickname.value = res.data.nickname
        avatar.value = res.data.figureurl_2
      })
    }
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 发送验证码
    const { countTime, start } = useCountDown()
    const send = async () => {
      // 当倒计时为0了才能调用
      if (countTime.value === 0) {
      // 得有手机号才发
        if (/^1[3-9]\d{9}$/.test(form.mobile)) {
        // 校验通过才能发验证码
          const res = await reqUserQQBindCode(form.mobile)
          console.log('res', res)
          // 调用接口成功之后 需要倒计时
          start()
          console.log('开启倒计时了')
        } else {
          message({
            type: 'error',
            text: '你手机号不是地球的'
          })
        }
      }
    }

    // 绑定手机
    const submit = async () => {
      if (!/^1[3-9]\d{9}$/.test(form.mobile) || form.code === '') return
      const res = await reqUserQQBindLogin({
        unionId: props.openId,
        ...form
      })
      console.log('绑定成功了', res)
      // 1. 把当前登录成功返回的用户数据 通过提交mutation 存到vuex里
      store.commit('user/setProfile', res.result)
      // 2. 跳转到首页
      router.push('/')
      message({
        type: 'success',
        text: 'QQ登录成功'
      })
    }
    return {
      nickname,
      avatar,
      form,
      countTime,
      send,
      submit
    }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
